using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using mfRCF.Core;
using mfRCF.Core.Extensions;
using mfXmlSerializer;

namespace OST.Commands
{
    using Extensions;
    /// <summary>
    /// setts the current datetime
    /// </summary>
    public class DatePOSTCommand : mfRcfCommand
    {
        #region CTOR
        public DatePOSTCommand(string httpPath)
            : base(httpPath, new mfPOSTMethod())
        {
            ContentTypeOut = "text/plain";
        }
        #endregion

        #region mfRcfCommand
        public override void ExecuteCommand(mfIHttpContext httpContext)
        {
            try
            {
                var requestString = httpContext.Request.InputStream.ReadAllText();

                Debug.Print(GetType() + " received: " + requestString);

                //referenced from mfXmlSerializer
                var newDateTime = requestString.ConvertToDateTime();

                Utility.SetLocalTime(newDateTime);

                //write response
                this.WriteHttpResponseOK(httpContext, "new Date " + newDateTime + " received");
            }
            catch (Exception ex)
            {
                this.WriteHttpResponseError(httpContext, ex);
            }
        }
        #endregion
    }
}
